<!DOCTYPE html>






  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.2.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.2.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.2.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.2.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta property="og:type" content="website">
<meta property="og:title" content="Bison分享日志">
<meta property="og:url" content="http://blog.bisondev.cn/index.html">
<meta property="og:site_name" content="Bison分享日志">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Bison分享日志">






  <link rel="canonical" href="http://blog.bisondev.cn/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Bison分享日志</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Bison分享日志</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">I'm not perfect,but I keep trying</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>




<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home menu-item-active">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-schedule">
    <a href="/schedule/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />日程表</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-sitemap">
    <a href="/sitemap.xml" rel="section">
      <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />站点地图</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2018/04/25/Java中的IO流/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/04/25/Java中的IO流/" itemprop="url">
                  未命名
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-04-25 10:41:43" itemprop="dateCreated datePublished" datetime="2018-04-25T10:41:43+08:00">2018-04-25</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2017-10-21 12:15:08" itemprop="dateModified" datetime="2017-10-21T12:15:08+08:00">2017-10-21</time>
              
            
          </span>

          

          
            
          

          
          
             <span id="/2018/04/25/Java中的IO流/" class="leancloud_visitors" data-flag-title="">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="一、IO流的分类"><a href="#一、IO流的分类" class="headerlink" title="一、IO流的分类"></a>一、IO流的分类</h1><ol>
<li><p>流向<br> 输入流与 输出流  </p>
</li>
<li><p>数据<br>字节流：二进制（可以传输一切文件，包括纯文本，音频、视频等）<br>字符流：文本文件，只能处理纯文本</p>
</li>
<li><p>功能<br> 节点流：包裹源头<br> 处理流：增强功能，提供性能</p>
</li>
</ol>
<h1 id="二、IO流基本步骤"><a href="#二、IO流基本步骤" class="headerlink" title="二、IO流基本步骤"></a>二、IO流基本步骤</h1><ol>
<li><p>创建源（文件/网络）</p>
</li>
<li><p>选择流</p>
</li>
<li><p>操作（读取/写出）</p>
</li>
<li><p>释放资源</p>
</li>
</ol>
<h1 id="三、流的种类"><a href="#三、流的种类" class="headerlink" title="三、流的种类"></a>三、流的种类</h1><h2 id="1-节点流"><a href="#1-节点流" class="headerlink" title="1. 节点流"></a>1. 节点流</h2><p>###(１) 字节流</p>
<p>####１) 输入流（InputStream / FileInputStream / ByteArrayInputStream）<br>    A. 操作：read（字节数组）<br>            a) 中间容器<br>                byte[] flush = new byte[长度];</p>
<pre><code>        b) 接收长度
            int len = 0;

        c) 循环读取
            while( -1 != (len = 流.read(flush))){}

        d) 操作
            输出或者拷贝

B. 重点方法
        a) read(byte[] b)
        b) read(byte[] b, int offset, int len)
        c) close()
</code></pre><p>####２) 输出流（OutputStream / FileOutputStream / ByteArrayInputStream）<br>    A. 操作：write（字节数组， 0， 长度），输出</p>
<pre><code>B. 重点方法
        a) write(byte[] b)
        b) write(byte[] b, int off, int len)
        c) flush();
        d) close();
</code></pre><h3 id="２-字符流"><a href="#２-字符流" class="headerlink" title="(２) 字符流"></a>(２) 字符流</h3><p>####１) 输入流（Reader / FileReader）<br>    A. 操作<br>            //1、创建源<br>            File src =new File(“E:/xp/test/a.txt”);<br>            //2、选择流（FileReader）<br>            Reader reader =null;<br>            try {<br>                    reader =new FileReader(src);<br>                    //3、读取操作<br>                    char[] flush =new char[1024];<br>                    int len =0;<br>                    //循环读取<br>                    while(-1 != (len = reader.read(flush))){<br>                    //4、处理数据（字符数组转成 字符串）<br>                    String str =new String(flush,0,len);<br>                    System.out.println(str);<br>                  }<br>            } catch (FileNotFoundException e) {<br>                   e.printStackTrace();<br>                   System.out.println(“源文件不存在”);<br>            } catch (IOException e) {<br>                   e.printStackTrace();<br>                   System.out.println(“文件读取失败”);<br>            }finally{<br>                   try {<br>                           if (null != reader) {<br>                           //5、关闭流<br>                           reader.close();<br>                           }<br>                       } catch (Exception e2) {<br>                       }<br>            }</p>
<pre><code>B. 重点方法
        a) read(char[] cbuf )
        b) read(char[] cbuf, int off, int len)
        c) close()
</code></pre><p>####２) 输出流（Writer / FileWriter）<br>    A. 操作<br>            //1、创建源<br>            File dest = new File(“e:/xp/test/char.txt”);<br>            //2、选择流<br>            Writer wr = null;<br>            try {<br>                    //追加文件，而不是覆盖文件<br>                    wr = new FileWriter(dest);<br>                    //3、写出<br>                    String msg = “追加…..锄禾日当午\r\n码农真辛苦\r\n一本小破书\r\n一读一上午”;<br>                    wr.write(msg);<br>                    wr.append(“倒萨发了看电视剧 “);<br>                    wr.flush();<br>            } catch (FileNotFoundException e) {<br>                    e.printStackTrace();<br>            } catch (IOException e) {<br>                    e.printStackTrace();<br>            } finally {<br>                    try {<br>                            if (null != wr) {<br>                            //4、关闭流<br>                            wr.close();<br>                            }<br>                     } catch (Exception e2) {<br>                     }<br>            }</p>
<pre><code>B. 重点方法
        a) write(char[] cbuf)
        b) write(char[] cbuf, int off, int len)
        c) flush()
        d) close()
</code></pre><p>##2. 处理流<br>处理流：增强功能、提供性能，节点流之上</p>
<p>###(１) 缓冲流（BufferedInputStream / BufferedOutputStream ， BufferedReader / BufferedWrite）</p>
<p>####１) 字节缓冲流（BufferedInputStream / BufferedOutputStream）</p>
<p>####２) 字符缓冲流（BufferedReader / BufferedWriter）<br>        a. 重点方法<br>                a) readLine();<br>                b) newLine();</p>
<p>####３) 代码示例<br>        a. 字节流 + 缓冲流<br>                //1、建立联系 源(存在且为文件) +目的地(文件可以不存在)<br>                File src =new File(srcPath);<br>                File dest =new File(destPath);<br>                if(! src.isFile()){ //不是文件或者为null<br>                    System.out.println(“只能拷贝文件”);<br>                    throw new IOException(“只能拷贝文件”);<br>                }<br>                //2、选择流<br>                InputStream is = new BufferedInputStream(new FileInputStream(src));<br>                OutputStream os = new BufferedOutputStream( new FileOutputStream(dest));<br>                //3、文件拷贝   循环+读取+写出<br>                byte[] flush =new byte[1024];<br>                int len =0;<br>                //读取<br>                try {<br>                        while(-1!=(len=is.read(flush))){<br>                        //写出<br>                        os.write(flush, 0, len);<br>                    }<br>                 } catch (IOException e) {<br>                        e.printStackTrace();<br>                 }<br>                 os.flush(); //强制刷出<br>                 //4、关闭流<br>                 os.close();<br>                 try {<br>                         is.close();<br>                 } catch (IOException e) {<br>                         e.printStackTrace();<br>                 }</p>
<pre><code>b. 字符流 + 缓冲流
         //1、创建源 仅限于 字符的纯文本
         File src = new File(&quot;E:/xp/test/Demo03.java&quot;);
         File dest = new File(&quot;e:/xp/test/char.txt&quot;);
         //2、选择流
         BufferedReader reader = null;
         BufferedWriter wr = null;
         try {
                 reader = new BufferedReader(new FileReader(src));
                 wr = new BufferedWriter(new FileWriter(dest));
                 //3、读取操作
                 /*
                 char[] flush =new char[1024];
                 int len =0;
                 while(-1!=(len=reader.read(flush))){
                     wr.write(flush, 0, len);
                 }*/
                 //新增方法的操作
                 String line = null;
                 while(null != (line = reader.readLine())){
                     wr.write(line);
                     //wr.append(&quot;\r\n&quot;);
                     wr.newLine(); //换行符号
                 }
                 wr.flush();//强制刷出
         } catch (FileNotFoundException e) {
                 e.printStackTrace();
                 System.out.println(&quot;源文件不存在&quot;);
         } catch (IOException e) {
                 e.printStackTrace();
                 System.out.println(&quot;文件读取失败&quot;);
         }finally{
                 //4、关闭流
                 try {
                         if (null != wr) {
                         wr.close();
                     }
                } catch (Exception e2) {
                }
                try {
                        if (null != reader) {
                        reader.close();
                }
                } catch (Exception e2) {
                }
         }
</code></pre><p>###(２) 转换流（InputStreamReader / OutputStreamWriter）<br>功能：字节流转为字符流，处理乱码（编码集、解码集）<br>概念：</p>
<ul>
<li>编码：字符       —-编码字符集&gt;&gt;&gt;   二进制</li>
<li>解码：二进制   —-解码字符集&gt;&gt;&gt;  字符</li>
<li>乱码：编码和解码的字符集不统一，字节缺少，长度丢失</li>
</ul>
<p>编码与解码代码示例</p>
<pre><code>//指定解码字符集
BufferedReader br =new BufferedReader(
new InputStreamReader(                      //3).转换流
              new BufferedInputStream(            //2).缓冲流
              new FileInputStream(        //1).文件输入流
              new File(&quot;E:/xp/test/Demo03.java&quot;))),&quot;UTF-8&quot;)
            );
//写出文件 编码
BufferedWriter bw =new BufferedWriter(
              new OutputStreamWriter(
              new BufferedOutputStream(
              new FileOutputStream(new File(&quot;E:/xp/test/encode.java&quot;)))));

String info =null;
while(null!=(info=br.readLine())){
        //System.out.println(info);
        bw.write(info);
        bw.newLine();
}
//关闭流
bw.flush();
bw.close();
br.close();
</code></pre><p>###(３) 其他处理流</p>
<p>####１) 基本类型处理流</p>
<p>#####a. 从文件流中读取</p>
<p>######a) 输入流 DataInputStream  readXxx()<br>        //创建源<br>        File src =new File(destPath);<br>        //选择流<br>        DataInputStream dis =new DataInputStream(<br>                      new BufferedInputStream(<br>                      new FileInputStream(src)<br>                      )<br>        );</p>
<pre><code>//操作 读取的顺序与写出一致   必须存在才能读取
//不一致，数据存在问题
long num2 =dis.readLong();
double num1 =dis.readDouble();
String str =dis.readUTF();

dis.close();
System.out.println(num2+&quot;--&gt;&quot;+str);
</code></pre><p>######b) 输出流 DataOutputStream writeXxx()<br>        double point =2.5;<br>        long num=100L;<br>        String str =”数据类型”;</p>
<pre><code>//创建源
File dest =new File(destPath);
//选择流  DataOutputStream
DataOutputStream dos =new DataOutputStream(
                  new BufferedOutputStream(
                  new FileOutputStream(dest)
                  )
            );
//操作 写出的顺序 为读取准备
dos.writeDouble(point);
dos.writeLong(num);
dos.writeUTF(str);
dos.flush();

//释放资源
dos.close();
</code></pre><p>#####b. 从字节流中读取</p>
<p>######a) 输入流 DataInputStream<br>        //选择流<br>        DataInputStream dis =new DataInputStream(<br>                         new BufferedInputStream(<br>                         new ByteArrayInputStream(src)<br>                         )<br>                      );</p>
<pre><code>//操作 读取的顺序与写出一致   必须存在才能读取
double num1 =dis.readDouble();
long num2 =dis.readLong();
String str =dis.readUTF();

dis.close();

System.out.println(num1+&quot;--&gt;&quot;+num2+&quot;--&gt;&quot;+str);
</code></pre><p>######b) 输出流 DataOutputStream<br>        //目标数组<br>        byte[] dest =null;<br>        double point =2.5;<br>        long num=100L;<br>        String str =”数据类型”;</p>
<pre><code>//选择流 ByteArrayOutputStream  DataOutputStream
ByteArrayOutputStream bos =new ByteArrayOutputStream();
DataOutputStream dos =new DataOutputStream(
                   new BufferedOutputStream(
                   bos
                   )
               );
//操作 写出的顺序 为读取准备
dos.writeDouble(point);
dos.writeLong(num);
dos.writeUTF(str);      
dos.flush();
//释放资源
dos.close();
//转换成字节数组
dest = bos.toByteArray();
</code></pre><p>####２) 引用类型（对象）处理流<br>注意：</p>
<ul>
<li>先序列化后反序列化，反序列化顺序必须与序列化一致</li>
<li>不是说是有的对象都可以序列化，必须继承 java.io.Serializable才可以</li>
<li>不是所有的属性都需要序列化，用 transient 标注不需要序列化的变量</li>
</ul>
<p>序列化的实体类代码示例</p>
<pre><code>public class Employee implements java.io.Serializable {
        //不需要序列化
        private transient String name;
        private double salary;
        public Employee() {
        }
        public Employee(String name, double salary) {
            super();
            this.name = name;
            this.salary = salary;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getSalary() {
            return salary;
        }
        public void setSalary(double salary) {
            this.salary = salary;
        }
}
</code></pre><p>#####a. 输入流（反序列化）<br>    //创建源<br>    File src =new File(destPath);<br>    //选择流<br>    ObjectInputStream dis =new ObjectInputStream(<br>                        new BufferedInputStream(<br>                        new FileInputStream(src)<br>                        )<br>                  );</p>
<pre><code>//操作 读取的顺序与写出一致   必须存在才能读取
//不一致，数据存在问题
Object obj = dis.readObject();
if(obj instanceof Employee){
    Employee emp = (Employee)obj;
    System.out.println(emp.getName());
    System.out.println(emp.getSalary());
}

obj = dis.readObject();
int[] arr = (int[])obj;
System.out.println(Arrays.toString(arr));
dis.close();
</code></pre><p>#####b. 输出流（序列化）<br>    Employee emp = new Employee(“bjsxt”,1000000);<br>    int[] arr ={1,2,3,45};<br>    //创建源<br>    File dest = new File(destPath);<br>    //选择流  ObjectOutputStream<br>    ObjectOutputStream dos = new ObjectOutputStream(<br>                        new BufferedOutputStream(<br>                        new FileOutputStream(dest)<br>                        )<br>                   );<br>    //操作 写出的顺序 为读取准备<br>    dos.writeObject(emp);<br>    dos.writeObject(arr);<br>    //释放资源<br>    dos.close();</p>
<p>####３) 打印流</p>
<p>#####a. 输入流<br>    InputStream is = System.in;  //键盘输入<br>    //文件内容输入<br>    is = new BufferedInputStream(new FileInputStream(“e:/xp/test/print.txt”));<br>    Scanner sc = new Scanner(is);<br>    //System.out.println(“请输入:”);<br>    System.out.println(sc.nextLine());</p>
<p>#####b. 输出流</p>
<p>######a) 输出到文件<br>        System.out.println(“test”);<br>        PrintStream ps =System.out;<br>        ps.println(false);</p>
<pre><code>//输出到文件
File src = new File(&quot;e:/xp/test/print.txt&quot;);
ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(src)));
ps.println(&quot;io is so easy....&quot;);

ps.close();
</code></pre><p>######b) 切换输出流与控制台和文件流之间<br>        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(“e:/xp/test/print.txt”)),true));<br>        System.out.println(“a”);  //控制台  –&gt;文件<br>        System.out.println(“test”);<br>        //回控制台<br>        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));<br>        System.out.println(“back….”);</p>
<h1 id="四、关闭流的工具类"><a href="#四、关闭流的工具类" class="headerlink" title="四、关闭流的工具类"></a>四、关闭流的工具类</h1><pre><code>public class FileUtil {

    /**
     * 工具类关闭流
     * 可变参数: ...  只能形参最后一个位置,处理方式与数组一致
     */
    public static void close(Closeable ... io){
        for(Closeable temp:io){
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (Exception e) {
            }
        }

    }

    /**
     * 使用泛型方法
     */
    public static &lt;T extends Closeable&gt; void closeAll(T ... io){
        for(Closeable temp:io){
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (Exception e) {
            }
        }
    }

}
</code></pre>
          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/12/05/JaveWeb环境配置/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/12/05/JaveWeb环境配置/" itemprop="url">
                  JaveWeb环境配置
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-12-05 12:08:31 / 修改时间：14:31:48" itemprop="dateCreated datePublished" datetime="2017-12-05T12:08:31+08:00">2017-12-05</time>
            

            
              

              
            
          </span>

          

          
            
          

          
          
             <span id="/2017/12/05/JaveWeb环境配置/" class="leancloud_visitors" data-flag-title="JaveWeb环境配置">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="1-申请云服务器"><a href="#1-申请云服务器" class="headerlink" title="1. 申请云服务器"></a>1. 申请云服务器</h2><h2 id="2-安装CentOS7-2镜像"><a href="#2-安装CentOS7-2镜像" class="headerlink" title="2.安装CentOS7.2镜像"></a>2.安装CentOS7.2镜像</h2><h2 id="3-防火墙设置"><a href="#3-防火墙设置" class="headerlink" title="3.防火墙设置"></a>3.防火墙设置</h2><p>3.1  开启防火墙<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl start firewalld</span><br><span class="line">systemctl status firewalld</span><br></pre></td></tr></table></figure></p>
<p>3.2 开启80和8080端口<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --add-port=80/tcp --permanent</span><br><span class="line">firewall-cmd --add-port=8080/tcp --permanent</span><br></pre></td></tr></table></figure></p>
<p>额外配置：  </p>
<ul>
<li><p>关闭防火墙  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld.service             #停止firewall  </span><br><span class="line">systemctl disable firewalld.service        #禁止firewall开机启动</span><br></pre></td></tr></table></figure>
</li>
<li><p>开启端口  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --zone=public --add-port=80/tcp --permanent</span><br></pre></td></tr></table></figure>
<p>命令含义：<br>–zone #作用域<br>–add-port=80/tcp #添加端口，格式为：端口/通讯协议<br>–permanent #永久生效，没有此参数重启后失效  </p>
</li>
<li>重启防火墙  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>常用命令介绍<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --state                           ##查看防火墙状态，是否是running  </span><br><span class="line">firewall-cmd --reload                          ##重新载入配置，比如添加规则之后，需要执行此命令  </span><br><span class="line">firewall-cmd --get-zones                       ##列出支持的zone  </span><br><span class="line">firewall-cmd --get-services                    ##列出支持的服务，在列表中的服务是放行的  </span><br><span class="line">firewall-cmd --query-service ftp               ##查看ftp服务是否支持，返回yes或者no  </span><br><span class="line">firewall-cmd --add-service=ftp                 ##临时开放ftp服务  </span><br><span class="line">firewall-cmd --add-service=ftp --permanent     ##永久开放ftp服务  </span><br><span class="line">firewall-cmd --remove-service=ftp --permanent  ##永久移除ftp服务  </span><br><span class="line">firewall-cmd --add-port=80/tcp --permanent     ##永久添加80端口  </span><br><span class="line">iptables -L -n                                 ##查看规则，这个命令是和iptables的相同的  </span><br><span class="line">man firewall-cmd                               ##查看帮助  </span><br><span class="line">更多命令，使用  firewall-cmd --help 查看帮助文件</span><br></pre></td></tr></table></figure></p>
<ul>
<li><p>CentOS 7.0默认使用的是firewall作为防火墙，使用iptables必须重新设置一下</p>
<ul>
<li><p>直接关闭防火墙  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld.service    #停止firewall  </span><br><span class="line">systemctl disable firewalld.service #禁止firewall开机启动</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置 iptables service  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install iptables-services</span><br></pre></td></tr></table></figure>
<p>如果要修改防火墙配置，如增加防火墙端口3306  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi /etc/sysconfig/iptables</span><br></pre></td></tr></table></figure>
<p>增加规则  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</span><br></pre></td></tr></table></figure>
<p>保存退出后  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl restart iptables.service #重启防火墙使配置生效  </span><br><span class="line">systemctl enable iptables.service #设置防火墙开机启动</span><br></pre></td></tr></table></figure>
<p>最后重启系统使设置生效即可。  </p>
</li>
</ul>
</li>
</ul>
<h2 id="4-添加系统用户"><a href="#4-添加系统用户" class="headerlink" title="4.添加系统用户"></a>4.添加系统用户</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">useradd bison         #添加一个bison用户</span><br><span class="line">passed bison          #给bison用户添加一个密码</span><br></pre></td></tr></table></figure>
<h2 id="5-给新用户设置系统管理员权限"><a href="#5-给新用户设置系统管理员权限" class="headerlink" title="5.给新用户设置系统管理员权限"></a>5.给新用户设置系统管理员权限</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">visudo                #编辑系统管理权限</span><br><span class="line"></span><br><span class="line">#修改</span><br><span class="line">root    ALL=(ALL)     ALL</span><br><span class="line">bison   ALL=(ALL)     ALL     #给bison用户添加系统管理员权限</span><br></pre></td></tr></table></figure>
<h2 id="6-安装JDK"><a href="#6-安装JDK" class="headerlink" title="6.安装JDK"></a>6.安装JDK</h2><p>6.1 传安装包到服务器<br>通过WinSCP传.tar.gz包到/home/bison中<br>6.2 解压<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf jdk-8u151-linux-x64.tar.gz</span><br></pre></td></tr></table></figure></p>
<h2 id="7-新建软件文件夹"><a href="#7-新建软件文件夹" class="headerlink" title="7.新建软件文件夹"></a>7.新建软件文件夹</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /user</span><br><span class="line">mkdir software</span><br><span class="line">chmod 777 software</span><br><span class="line"># 把JDK文件夹复制进software中</span><br><span class="line">mv /home/bison/jdk1.8.0_151/ ./software/</span><br></pre></td></tr></table></figure>
<h2 id="8-修改环境变量"><a href="#8-修改环境变量" class="headerlink" title="8.修改环境变量"></a>8.修改环境变量</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">vim .bash_profile</span><br><span class="line"></span><br><span class="line">#添加JAVA_HOME</span><br><span class="line">PATH=$PATH:$HOME/bin</span><br><span class="line"></span><br><span class="line">export JAVA_HOME=/usr/software/jdk1.8.0_151</span><br><span class="line">export PATH=$PATH:$JAVA_HOME/bin</span><br><span class="line"></span><br><span class="line">export PATH</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#更新配置文件</span><br><span class="line">source .bash_profile</span><br></pre></td></tr></table></figure>

          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/07/30/小米路由器青春版折腾记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/07/30/小米路由器青春版折腾记/" itemprop="url">
                  小米路由器青春版折腾记
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-07-30 22:56:06" itemprop="dateCreated datePublished" datetime="2017-07-30T22:56:06+08:00">2017-07-30</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2017-07-31 00:57:41" itemprop="dateModified" datetime="2017-07-31T00:57:41+08:00">2017-07-31</time>
              
            
          </span>

          

          
            
          

          
          
             <span id="/2017/07/30/小米路由器青春版折腾记/" class="leancloud_visitors" data-flag-title="小米路由器青春版折腾记">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>经过十几天的生产实习，学习了嵌入式开发的基本流程，从虚拟机到开发版，未知的事物总是促使我们去不断学习。伪入门嵌入式后，现在看一切硬件有关的东西，脑海都浮现嵌入式三个字。- _ -</p>
<p>放暑假的宿舍还是十分清净的，但是，你是否在电脑浏览网页的时候，看到一个个浮动的广告，没点广告还弹出一系列广告，心里不断谩骂该网站的前端开发者，不按套路出牌。在你用手机等移动设备浏览网页时，广告还是来了，它没改头换面，没错，它的大小可能都还没有适配好，戳关闭按钮也关不掉的那种，你可能在心里又不经吐槽网页的兼容性。当你追一部大剧的时候，你可能会体会到广告两小时，正片5分钟的痛感，恐怕你就在这种生活场景中发现了JS的强大。</p>
<p>好啦，不扯了。对于非夏天晚上断电的宿舍来说，小米路由器青春版无疑是非常实用的。体积小，丢在角落没烦恼；价格便宜，学生党买得起；配备Miscro USB，充电宝也能供电，不惧断电。呃呃呃，没收广告费。小米路由器青春版搭载的是基于OpenWRT深度定制的智能路由器操作系统MiWiFi ROM，说到底，就是linux啦，就是嵌入式的体现吧。</p>
<h1 id="二、进入路由器内部"><a href="#二、进入路由器内部" class="headerlink" title="二、进入路由器内部"></a>二、进入路由器内部</h1><p>这里说的内部不是硬件的内部，我们学软件的，就在能不拆开硬件解决问题就不拆。既然是类似于linux的系统，那应该有命令行才像呀，那我们就找找。小米的产品还是挺开放的，路由也是有开放平台的。<a href="http://www1.miwifi.com/miwifi_open.html" target="_blank" rel="noopener">MiWifi开放平台</a></p>
<p>在开放平台中，我们很容易就能找到”开启SSH工具”，玩过服务器的都知道，用SSH的方式，我们可以通过命令行的方式与系统进行连接，路由器系统也不例外。出于安全考虑，路由器系统都会把SSH关了，自然我们就不容易进到路由器的系统中。在寻找官方的开启SSH工具的过程中我们会发现，我们的小米路由器青春版其实是没有官方的开启工具的。<br><img src="http://opl5tlfw9.bkt.clouddn.com/17-7-30/21766768.jpg" alt=""><br>这个时候在查阅网友的帖子发现，在旧版的开发版系统中，是可以直接通过修改路由管理器的请求地址的特定参数键值对，而改变系统参数。 </p>
<p>下面引用修改的方法：  </p>
<ol>
<li><p>登录路由管理界面<br>地址栏输入192.168.31.1，输入账号密码后，地址栏会多了一串参数，类似于<br><code>http://192.168.31.1/cgi-bin/luci/;stok=075a9192918557c27cdbcae2175281d9/web/home#router</code>   </p>
</li>
<li><p>更改管理员密码和系统的root密码<br>将以上的地址URL中<code>/web/home#router</code>改成<code>/api/xqsystem/set_name_password?oldPwd=当前路由的密码&amp;newPwd=新的路由密码</code>，<br>然后查看网页的返回结果，如果返回的JSON字符串是<code>{&quot;code&quot;:0}</code>，那就任务完成，如果返回是说参数错误的，那十有八九是系统是新版，小米官方修补了这个漏洞。<br>解决办法就是刷回旧版系统，鉴于找资源麻烦，这里做了收集，以下是官方的小米路由器青春版的2.1.22的开发版ROM。  <a href="http://pan.baidu.com/s/1bpvnY8Z" title="链接" target="_blank" rel="noopener">链接</a>，密码：yred（刷机方法由官方的路由管理界面，上传本ROM包，自动升级即可）</p>
</li>
<li><p>启用路由的telnet登录方式<br>同第二步中的修改网址URL，把<code>/web/home#router</code>改为<code>/api/xqnetwork/set_wifi_ap?ssid=xiaomi&amp;encryption=NONE&amp;enctype=NONE&amp;channel=1%3B%2Fusr%2Fsbin%2Ftelnetd</code>，然后查看返回的JSON数据<code>{&quot;msg&quot;:&quot;未能连接到指定WiFi(Probe timeout)&quot;,&quot;code&quot;:1616}</code>，返回码有可能不同，但是这里已经可以通过telnet的方式来登录路由器了</p>
</li>
<li><p>用putty的telnet方式登录<br>下载一个<a href="http://pan.baidu.com/s/1hsquhxy" target="_blank" rel="noopener">Putty</a> ，密码：gqxa。然后打开选填以下参数，连接类型：telnet;主机名称：192.168.31.1.打开后看到login，输入root，密码则为以上第二步中修改的新密码。然后依次执行下面的三条指令：<br>4.1 <code>sed -i &quot;:x;N;s/if \[.*\; then\n.*return 0\n.*fi/#tb/;b x&quot; /etc/init.d/dropbear</code><br>4.2 <code>/etc/init.d/dropbear start</code><br>4.3 <code>nvram set ssh_en=1; nvram commit</code>  </p>
</li>
</ol>
<h1 id="三、进阶"><a href="#三、进阶" class="headerlink" title="三、进阶"></a>三、进阶</h1><ol>
<li><p>备份原厂bin（可选）<br>1.1 打开WINSCP文件协议SCP 主机名192.168.31.1 端口号22用户名root密码admin（此处由上面设置了什么新密码决定） 。有错误提示就点击更新或者确定<br>1.2 打开putty 主机名称192.168.31.1 端口号22 连接类型ssh,有错误提示就点击更新或者确定<br>输入命令 <code>cat /proc/mtd</code><br><img src="http://opl5tlfw9.bkt.clouddn.com/17-7-31/89106099.jpg" alt=""><br>可以看到上面有十个固件和分区，看名字可以知道第一个mtd0固件包含已全部分区的数据（All），第二个是Bootloader，刷过机的都会对这个有映像。<br><code>dd if=/dev/mtd0 of=/tmp/all.bin</code>，这是表示备份第一个固件到tmp文件夹的all.bin文件中，另外类似的命令有：<code>dd if=/dev/mtd1 of=/tmp/bootloader.bin</code>等。（需要注意的是，路由器的空间很小，尽可能备份之后，通过winscp下载并删了路由中的文件，以防空间不足）</p>
</li>
<li><p>刷入breed<br>通过WinScp把breed.bin（地址：<a href="http://pan.baidu.com/s/1nuY9aPj" target="_blank" rel="noopener">http://pan.baidu.com/s/1nuY9aPj</a>，密码：x1qa）传入到/tmp中。然后通过Putty，输入命令<code>mtd -r write /tmp/breed.bin Bootloader</code>写入Breed，刷入成功后会提示重启，putty和WinScp都会断开，关闭了即可。</p>
</li>
<li><p>进入breed控制台<br>把电源拔了，使路由关机，用牙签或者其他尖锐物戳着reset键，然后插上插头，待路由器后方的灯（非正前方的灯）闪烁时松开reset键即可，然后用一条网线把电脑和路由器的WAN口相连接，打开浏览器访问192.168.1.1，即可进入breed控制台了。</p>
</li>
<li><p>刷机（可选）<br>刷机只是尝试不同的系统，和不同的功能。在本次尝鲜中，刷入了PandoraBox（潘多拉魔盒）系统，链接：<a href="http://pan.baidu.com/s/1cKs4W2" target="_blank" rel="noopener">http://pan.baidu.com/s/1cKs4W2</a> 密码：dygy。刷入完成后最简单的测试方法就是在地址栏输入192.168.1.1测试路由是否开启成功，成功的话，会进入PandoraBox的管理界面中。</p>
</li>
</ol>
<h1 id="四、基本使用"><a href="#四、基本使用" class="headerlink" title="四、基本使用"></a>四、基本使用</h1><ol>
<li><p>设置管理界面样式<br>系统-&gt;系统-&gt;语言和界面-&gt;主题，我是选择了Bootstrap样式，这样好看多了</p>
</li>
<li><p>设置PPoE上网方式<br>网络-&gt;接口-&gt;WAN-&gt;修改-&gt;协议，选择PPPoE后，点击切换协议，然后在PAP/CHAP用户名中输入PPoE的账号，在密码中输入上网的密码，然后直接点击保存&amp;应用，就会生效</p>
</li>
<li><p>添加广告屏蔽插件<br>系统-&gt;软件包-&gt;刷新列表-&gt;可用软件包-&gt;选择L-&gt;安装“luci-app-adbyby”。<br><img src="http://opl5tlfw9.bkt.clouddn.com/17-7-31/69809948.jpg" alt=""></p>
</li>
</ol>
<p>安装完之后刷新界面，会在服务-&gt;广告屏蔽中发现广告屏蔽的状态<br><img src="http://opl5tlfw9.bkt.clouddn.com/17-7-31/34625424.jpg" alt=""></p>

          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/07/28/ListView的优化/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/07/28/ListView的优化/" itemprop="url">
                  ListView的优化
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-07-28 20:49:35 / 修改时间：22:42:58" itemprop="dateCreated datePublished" datetime="2017-07-28T20:49:35+08:00">2017-07-28</time>
            

            
              

              
            
          </span>

          

          
            
          

          
          
             <span id="/2017/07/28/ListView的优化/" class="leancloud_visitors" data-flag-title="ListView的优化">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>在开发和学习中，我们常常接触到RecyclerView。作为ListView的升级版，如果我们仅仅是使用RecyclerView而不去探究ListView有什么不足的地方的话，那是没用脑子思考的编程，也不会体会到RecyclerView有什么方便的地方。在很多书籍中都有提到ListView的优化，其中徐宜生的《Android群英传》和郭霖的《第一行代码》第2版中都有少量的篇幅介绍。好记性不如烂笔头，记下所学。</p>
<h1 id="二、ListView的使用"><a href="#二、ListView的使用" class="headerlink" title="二、ListView的使用"></a>二、ListView的使用</h1><p>ListView作为一个控件，那就可以和别的控件一样，在xml布局文件中添加即可<br>`</p>
<pre><code>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  
    android:layout_width=&quot;match_parent&quot; 
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;ListView
        android:id=&quot;@+id/list_view&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/LinearLayout&gt;
</code></pre><p>`</p>
<p>但是，大量的数据集合，是不能直接传递给ListView的，所以还是需要通过适配器来传递。</p>
<p>`</p>
<pre><code>public class MainActivity extends AppcompatActivity {

    private String[] data = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;,
        &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, &quot;P&quot;, &quot;Q&quot;, &quot;R&quot;, &quot;S&quot;, &quot;T&quot;, &quot;U&quot;, &quot;V&quot;, &quot;W&quot;, &quot;X&quot;,
        &quot;Y&quot;, &quot;Z&quot;};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //使用ArrayAdapter方式
        ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(
            MainActivity.this, 
            android.R.layout.simple_list_item_1, 
            data);

        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
}
</code></pre><p>`<br>由于只是简单的文本，所以使用ArrayAdapter就可以了，并使用了Android内置的布局文件(android.R.layout.simple_list_item_1)</p>
<p>当然，以上这些只是简单的使用，而且一个Item一个TextView可能看不出有什么效率。当使用复杂的Item布局时，可能就会看得出效果了。因为自定义的Adapter里面的getView方法，会重复地inflate布局，并且findViewById，这就是我们可以优化的地方。</p>
<h1 id="三、优化"><a href="#三、优化" class="headerlink" title="三、优化"></a>三、优化</h1><p>1、避免重复inflate布局<br>在getView方法中，传入了一个参数converView，该参数是之前加载好的布局的缓存，所以我们可以加以利用<br>`</p>
<pre><code>View view;    
if(converView == null) {  
    view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);  
} else {  
    view = converView;  
}  
</code></pre><p>`</p>
<p>2、避免频繁调用findViewById方法<br>在这里引入ViewHolder来作为解决方案，类似于RecyclerView的Viewholder。<br>`</p>
<pre><code>View view;  
ViewHolder viewHolder;
if(converView == null) {
    view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
    viewHolder = new ViewHolder();        //也可以把ViewHolder写成Adapter的静态内部类，就不用实例化了
    viewHolder.img = (ImageView) view.findViewById(R.id.iv_image);
    viewholder.name = (TextView) view.findViewById(R.id.tv_name);
    view.setTag(viewHolder);            //ViewHolder存储于View中
} else {
    view = converView;
    viewHolder = (ViewHolder) view.getTag();
}
viewHolder.img.setImageResource(data.getImgId());
viewHolder.name.setText(data.getName());
</code></pre><p>`</p>
<p>3、避免空ListView的差体验<br>设置一个FrameLayout，然后在ListView的后面添加一个ImageView的图片。然后再给ListView使用setEmptyView方法即可。</p>

          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/05/08/Android6-0-API-23-后的权限问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/05/08/Android6-0-API-23-后的权限问题/" itemprop="url">
                  Android 6.0(API 23)后的权限问题
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-05-08 17:30:08 / 修改时间：23:41:21" itemprop="dateCreated datePublished" datetime="2017-05-08T17:30:08+08:00">2017-05-08</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/05/08/Android6-0-API-23-后的权限问题/" class="leancloud_visitors" data-flag-title="Android 6.0(API 23)后的权限问题">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="一、Android-6-0后的权限问题"><a href="#一、Android-6-0后的权限问题" class="headerlink" title="一、Android 6.0后的权限问题"></a>一、Android 6.0后的权限问题</h2><p>谷歌发布的Android 6.0,与之前的版本相比，有了很多的亮点。诸如App Permissions（软件权限管理）、Chrome Custom Tabs（Chrome的网页浏览体验提升）、App Links(APP关联)、Android Pay(安卓支付)、Fingerprint Support(指纹支持)、Power &amp; Change(电量管理 )等等。但是，我们今天要说的是App Permissions,没错，就是它。  </p>
<p>相信在Android 6.0之前，安卓开发者可以为所欲为地在App中获取用户的隐私资料，而且是在你不知情的情况下。这就引发了很多对隐私安全的争论，谷歌也知道了这个问题，本着用户至上的原则，在Android 6.0中，Android系统加入了软件管理权限。用户用起来是放心了很多，但对于开发者，说多都是泪，当然，只是要在编程的过程中做多一些工作而已。</p>
<p>在Android中，如果你在App中的AndroidManifest.xml中没有声明软件的权限，你的App就没权限去做相应的事，这是众所周知的。但是在Android 6.0后，有些权限即使你在AndroidManifest.xml文件中添加了声明，也还是会报java.lang.SecurityException异常。因为这个时候你很可能申请了Dangerous Permissions（危险权限），何为危险权限，下面会进行说明。</p>
<h2 id="二、权限的分类"><a href="#二、权限的分类" class="headerlink" title="二、权限的分类"></a>二、权限的分类</h2><p>按照谷歌API Guides的说法，系统权限分类几个保护</p>
<h3 id="1-Normal-permissions"><a href="#1-Normal-permissions" class="headerlink" title="1. Normal permissions"></a>1. Normal permissions</h3><p>从字面翻译过来就是普通权限，但是API Guides中的中文版中译成了正常权限，这翻译有点生硬了些，为了方便理解，以下称为普通权限。<br>引用官方对Normal permissions的解释：</p>
<blockquote>
<p>Normal permissions cover areas where your app needs to access data or resources outside the app’s sandbox, but where there’s very little risk to the user’s privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app. For a full listing of the current normal permissions, see Normal permissions <a href="https://developer.android.com/guide/topics/permissions/normal-permissions.html" target="_blank" rel="noopener">Normal permissions</a>.</p>
</blockquote>
<p>也就是说，普通权限包括应用需要访问其沙盒（sandbox）外部数据或资源，也就是对用户隐私或其他应用操作风险很小的区域。例如，设置时区的权限就是普通权限，如果应用声明了其需要这些权限（其实也就是在AndroidManifest.xml中添加了声明），那么在使用中，系统会自动向应用授予该权限，并且用户无法撤销这些权限。<br>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/08/Android6-0-API-23-后的权限问题/#更多" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/05/07/RSS阅读神器——Inoreader/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/05/07/RSS阅读神器——Inoreader/" itemprop="url">
                  RSS阅读神器 —— Inoreader
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-05-07 21:22:57 / 修改时间：22:46:29" itemprop="dateCreated datePublished" datetime="2017-05-07T21:22:57+08:00">2017-05-07</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/生活/" itemprop="url" rel="index"><span itemprop="name">生活</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/05/07/RSS阅读神器——Inoreader/" class="leancloud_visitors" data-flag-title="RSS阅读神器 —— Inoreader">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h1 id="一、Inoreader简介"><a href="#一、Inoreader简介" class="headerlink" title="一、Inoreader简介"></a>一、Inoreader简介</h1><p>根据维基百科的介绍</p>
<blockquote>
<p>Inoreader是基于网络的内容和RSS摘要阅读器，适用于网络浏览器的云服务和运行iOS和Android的移动设备。它以统一的布局编辑用户的在线来源的新闻源，以自定义和与他人分享。Inoreader于2013年被Innologica首次发布</p>
</blockquote>
<blockquote>
<p>Inoreader is a web-based content and RSS feed reader, a cloud-based service for web browsers and mobile devices running iOS and Android.</p>
</blockquote>
<p>简单点来说，就是一个RSS阅读器，在这里，你可以订阅不同来源的新闻、通知、公示、视频、音乐等等任何可以订阅的东西，只要那个网站的访问没有太多的门槛。当然，这只是个工具，你拿来做什么，完全取决于你自己。</p>
<p>有意思的是，即使你没有科学上网，这个网站也是可以上的。  </p>
<h1 id="二、Inoreader基本使用"><a href="#二、Inoreader基本使用" class="headerlink" title="二、Inoreader基本使用"></a>二、Inoreader基本使用</h1><h2 id="1-注册"><a href="#1-注册" class="headerlink" title="1. 注册"></a>1. 注册</h2><p>首先你得注册个账号<a href="https://www.inoreader.com/" title="Inoreader官网" target="_blank" rel="noopener">Inoreader官网</a><br>官网如下图<br><img src="http://opl5tlfw9.bkt.clouddn.com/17-5-7/84899187-file_1494165213373_bc28.png" alt=""></p>
<h2 id="2-切换语言"><a href="#2-切换语言" class="headerlink" title="2. 切换语言"></a>2. 切换语言</h2><p>当然，你看不懂英语的话，右上角可以切换成简体中文  </p>
<h2 id="3-登录"><a href="#3-登录" class="headerlink" title="3. 登录"></a>3. 登录</h2><p><img src="http://opl5tlfw9.bkt.clouddn.com/17-5-7/21794436-file_1494165464909_105bf.png" alt=""><br>如果你嫌麻烦不想再创建账号，那直接用Facebook账号或者Google账号登录即可，注意科学上网。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/07/RSS阅读神器——Inoreader/#更多" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/04/30/Activity学习之LauncherActivity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/04/30/Activity学习之LauncherActivity/" itemprop="url">
                  Activity学习之LauncherActivity
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-04-30 16:23:51 / 修改时间：21:49:13" itemprop="dateCreated datePublished" datetime="2017-04-30T16:23:51+08:00">2017-04-30</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/30/Activity学习之LauncherActivity/" class="leancloud_visitors" data-flag-title="Activity学习之LauncherActivity">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h2><pre><code>java.lang.Object
   ↳    android.content.Context
        ↳    android.content.ContextWrapper
             ↳    android.view.ContextThemeWrapper
                  ↳    android.app.Activity
                       ↳    android.app.ListActivity
                            ↳    android.app.LauncherActivity
</code></pre><h2 id="二、简介"><a href="#二、简介" class="headerlink" title="二、简介"></a>二、简介</h2><p>Launcher Activity显示可以针对给定Intent执行的所有Activity的列表,点击即可启动</p>
<h2 id="二、理解"><a href="#二、理解" class="headerlink" title="二、理解"></a>二、理解</h2><p>在继承关系中我们可以发现，该Activity继承了ListActivity，所以布局同样是可以自定义的，不过要注意setContentView（）中设置的布局，应当包含一个ListView且id为”android:id/list”</p>
<p>还要注意的是，这个Activity要重写intentForPosition()方法，返回对应的Intent，这样就可以实现点击了</p>
<h2 id="三、完整示例"><a href="#三、完整示例" class="headerlink" title="三、完整示例"></a>三、完整示例</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestLauncher</span> <span class="keyword">extends</span> <span class="title">LauncherActivity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//Activity的名字</span></span><br><span class="line">    <span class="keyword">private</span> String[] actNames = &#123;<span class="string">"布局"</span>, <span class="string">"Activity"</span>, <span class="string">"硬件"</span>, <span class="string">"UI控件"</span>&#125;;</span><br><span class="line">    <span class="keyword">private</span> Class&lt;?&gt;[] clazzs = &#123;LayoutActivity.class,</span><br><span class="line">            ActListActivity.class,</span><br><span class="line">            HardActivity.class,</span><br><span class="line">            ControlActivity.class&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ListView listView;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        ArrayAdapter&lt;String&gt; adapter = <span class="keyword">new</span> ArrayAdapter&lt;String&gt;(<span class="keyword">this</span>, R.layout.home_item, actNames);</span><br><span class="line">        listView = getListView();</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> != listView) &#123;</span><br><span class="line">            <span class="comment">//设置该窗口显示的裂变所需的Adapter</span></span><br><span class="line">            listView.setAdapter(adapter);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据列表项返回指定Activity对应的Intent</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> position</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> Intent <span class="title">intentForPosition</span><span class="params">(<span class="keyword">int</span> position)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Intent(TestLauncher.<span class="keyword">this</span>, clazzs[position]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/04/30/Activity学习之ListActivity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/04/30/Activity学习之ListActivity/" itemprop="url">
                  Activity学习之ListActivity
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-04-30 16:17:16 / 修改时间：16:23:16" itemprop="dateCreated datePublished" datetime="2017-04-30T16:17:16+08:00">2017-04-30</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/30/Activity学习之ListActivity/" class="leancloud_visitors" data-flag-title="Activity学习之ListActivity">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h2><pre><code>java.lang.Object
   ↳    android.content.Context
        ↳    android.content.ContextWrapper
             ↳    android.view.ContextThemeWrapper
                  ↳    android.app.Activity
                       ↳    android.app.ListActivity
</code></pre><h2 id="二、简介"><a href="#二、简介" class="headerlink" title="二、简介"></a>二、简介</h2><p>ListActivity是一个可以通过绑定数据源（例如array或者Cursor）来显示项目列表的Activity，当用户选择Item时可以暴露事件的处理方法。它持有一个可以绑定不同数据源的ListView对象，通过array和Cursor保存查询结果。</p>
<h2 id="三、屏幕布局"><a href="#三、屏幕布局" class="headerlink" title="三、屏幕布局"></a>三、屏幕布局</h2><p>ListActivity有一个默认的布局，是由一个居中的全屏列表组成。如果要自定义布局的话，可以在onCreate()方法中通过setContentView（）方法来自定义布局。但是，该自定义的布局中，必须包含一个ListView对象，且id要设置为“@android：id / list”</p>
<p>或者，你的自定义View可以包含任何类型的另一个View对象，以便可以在List View为空的时候显示一些提示。这个“空列表”的通知View必须含有一个id为”adnroid:id/empty”。这个时候，当ListView没有数据的时候，列表视图将会隐藏。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:orientation</span>=<span class="string">"vertical"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:paddingLeft</span>=<span class="string">"8dp"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:paddingRight</span>=<span class="string">"8dp"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">ListView</span> <span class="attr">android:id</span>=<span class="string">"@android:id/list"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:background</span>=<span class="string">"#00FF00"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:layout_weight</span>=<span class="string">"1"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:drawSelectorOnTop</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">TextView</span> <span class="attr">android:id</span>=<span class="string">"@android:id/empty"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:background</span>=<span class="string">"#FF0000"</span></span></span><br><span class="line"><span class="tag">              <span class="attr">android:text</span>=<span class="string">"No data"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/04/30/Activity学习之ListActivity/#更多" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/04/30/Activity学习之ExpandableListActivity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/04/30/Activity学习之ExpandableListActivity/" itemprop="url">
                  Activity学习之ExpandableListActivity
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-04-30 16:09:04 / 修改时间：16:15:58" itemprop="dateCreated datePublished" datetime="2017-04-30T16:09:04+08:00">2017-04-30</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/30/Activity学习之ExpandableListActivity/" class="leancloud_visitors" data-flag-title="Activity学习之ExpandableListActivity">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h2><pre><code>java.lang.Object  
   ↳        android.content.Context  
            ↳        android.content.ContextWrapper  
                     ↳        android.view.ContextThemeWrapper  
                              ↳        android.app.Activity  
                                       ↳        android.app.ExpandableListActivity  
</code></pre><h2 id="二、简介"><a href="#二、简介" class="headerlink" title="二、简介"></a>二、简介</h2><p>一个可以通过绑定实现ExpandableListAdapter的数据源来显示项目的可扩展列表的Activity，并在用户选择Item时公开事件的处理方法。</p>
<p>ExpandableListActivity持有一个ExpandableListView对象，该对象可以绑定提供两级数据的不同数据源（顶级是组，每组下面都是子级）。</p>
<p>可展开的Activity，和ExpandableListView的用法相似，只要为该Activity传入一个ExpandableListAdapter对象即可，接下来ExpandableListActivity将会生成一个显示可展开列表的窗口</p>
<h2 id="三、屏幕布局"><a href="#三、屏幕布局" class="headerlink" title="三、屏幕布局"></a>三、屏幕布局</h2><p>ExpandableListActivity具有默认布局，该布局由单个全屏，居中可展开列表组成。 但是，如果需要，您可以通过在onCreate（）中设置自己的视图布局与setContentView（）来自定义屏幕布局。 为此，您自己的视图必须包含一个具有id“@android：id / list”的ExpandableListView对象</p>
<p>或者，您的自定义视图可以包含任何类型的另一个视图对象，以便在列表视图为空时显示。 这个“空列表”通知程序必须有一个id“android：empty”。 请注意，当存在空视图时，当没有数据显示时，可扩展列表视图将被隐藏。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/04/30/Activity学习之ExpandableListActivity/#更多" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bisondev.cn/2017/04/30/Activity学习之PreferenceActivity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Bison Qin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Bison分享日志">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/04/30/Activity学习之PreferenceActivity/" itemprop="url">
                  Activity学习之PreferenceActivity
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-04-30 15:37:15 / 修改时间：16:16:32" itemprop="dateCreated datePublished" datetime="2017-04-30T15:37:15+08:00">2017-04-30</time>
            

            
              

              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/04/30/Activity学习之PreferenceActivity/" class="leancloud_visitors" data-flag-title="Activity学习之PreferenceActivity">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h2><pre><code>java.lang.Object  
  ↳    android.content.Context  
     ↳    android.content.ContextWrapper  
          ↳    android.view.ContextThemeWrapper  
              ↳  android.app.Activity  
                 ↳ android.app.ListActivity  
                    ↳ android.preference.PreferenceActivity  
</code></pre><h2 id="二、简介"><a href="#二、简介" class="headerlink" title="二、简介"></a>二、简介</h2><p>当开发一个Android应用程序时，不可避免地需要进行选项设置，这些选项设置会以参数的形式保存，习惯上我们会用Preferences进行保存。Android3.0开始，Android推荐PreferenceActivity与PreferenceFragment结合使用，其中PreferenceActivity只负责加载选项设置列表的布局文件，PreferenceFragment才负责加载选项设置的布局文件</p>
<h2 id="三、选项布局子属性"><a href="#三、选项布局子属性" class="headerlink" title="三、选项布局子属性"></a>三、选项布局子属性</h2><ol>
<li>Preference（仅供显示的参数）  </li>
<li>PreferenceScreen（根元素）  </li>
<li><p>PreferenceCategory（参数组）<br> a. 主要属性<br> ○ tittle：参数组的标题</p>
</li>
<li><p>CheckBoxPreference（复选框参数）<br> a. 主要的属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summaryOn：当选中复选框时显示的提示短文<br> ○ summaryOff：当复选框没被选中时显示的提示短文<br> ○ defaultValue：复选框的默认状态  </p>
</li>
<li><p>EditTextPreference（文本框输入参数）<br> a. 主要属性<br> ○ key：保存在Preferences中的键值<br> ○ tittle：该选项设置的标题<br> ○ summary：提示的短文<br> ○ dialogTittle：弹出的对话框的标题<br> ○ dialogMessage：弹出的对话框的信息<br> ○ dialogIcon：弹出对话框的图标<br> ○ dialogLayout：弹出对话框的布局  </p>
</li>
</ol>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/04/30/Activity学习之PreferenceActivity/#更多" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Bison Qin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">8</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Bison Qin</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Pisces</a> v6.2.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.2.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.2.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.2.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.2.0"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.2.0"></script>



  



	





  





  










  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("M90nbI9KgVkqdeNl4kIE1aY4-gzGzoHsz", "3I8HRy0nT1nMlMw7enfvrnu5");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
              var $element = $(document.getElementById(url));
              $element.find('.leancloud-visitors-count').text(counter.get('time'));
            
            counter.save(null, {
              success: function(counter) {
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var $element = $(document.getElementById(url));
              $element.find('.leancloud-visitors-count').text('Counter not initialized! See more at console err msg.');
              console.error('ATTENTION! LeanCloud counter has security bug, see here how to solve it: https://github.com/theme-next/hexo-leancloud-counter-security. \n But you also can use LeanCloud without security, by set \'security\' option to \'false\'.');
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  
  

  

  

  

  

  

</body>
</html>
